package com.daz.common.publicDict.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.websocket.Session;

import org.springframework.beans.factory.annotation.Autowired;

import ch.qos.logback.core.joran.conditional.Condition;

import com.alibaba.fastjson.JSON;
import com.daz.common.publicDict.pojo.InquireMark;
import com.daz.common.publicDict.pojo.SystemParam;
import com.daz.common.publicDict.pojo.publicDictPojo;
import com.daz.common.publicDict.server.IPublicDictServer;
import com.daz.common.publicDict.server.impl.PublicDictServer;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class PublicDictAction extends ActionSupport{
	private String publicCode;
	private List<publicDictPojo> list;
	private Boolean success;
	private String msg;
	private String jsonString;
	private String delsString;
	private String valueId;
	private String paramId;
	private List<SystemParam> systemParamList;
	private List<InquireMark> markList;
	private String prom;
	@Autowired
	private IPublicDictServer publicDictServer;
	public String searchPublicDict(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("publicCode", publicCode);
		try {
			list = publicDictServer.searchPublicDict(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public Boolean isRepeat(publicDictPojo pojo){
		Map<String, Object> map = new HashMap<>();
		map.put("valueId", pojo.getValueId());
		map.put("publicCode", pojo.getPublicCode());
		publicDictPojo resultDictPojo=null;
		try {
			resultDictPojo = publicDictServer.searchOnePublicDict(map);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(resultDictPojo == null || pojo.getPublicId().equals(resultDictPojo.getPublicId())){
			return true;
		}else{
			return false;
		}
	}
	public String addSystemParam(){
		publicDictPojo pojo = JSON.parseObject(jsonString, publicDictPojo.class);
		try {
			if(isRepeat(pojo)){
				this.setSuccess(publicDictServer.addSystemParam(pojo));
			}
			else{
				this.setSuccess(false);
				this.setMsg("数据存在重复");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String updateSystemParam(){
		publicDictPojo pojo = JSON.parseObject(jsonString, publicDictPojo.class);
		try {
			if(isRepeat(pojo)){
				this.setSuccess(publicDictServer.updateSystemParam(pojo));
			}else{
				this.setSuccess(false);
				this.setMsg("数据存在重复");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String deleteSystemParam(){
		String[] ids = delsString.split(","); 
		List<String> list = new ArrayList<>();
		Collections.addAll(list, ids);
		try {
			this.setSuccess(publicDictServer.deleteSystemParam(list));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String selectStatus(){
		Map<String, Object> map = new HashMap<>();
		try {
			systemParamList = publicDictServer.selectStatus(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String updateStatus(){
		Map<String, Object> map = new HashMap<>();
		map.put("paramId", paramId);
		map.put("valueId",valueId);
		try {
			publicDictServer.updateStatus(map);
			this.setSuccess(true);
		} catch (Exception e) {
			this.setSuccess(false);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String seleteMarkByTeacherId(){
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> session = ActionContext.getContext().getSession();
		map.put("teacherId", session.get("userId"));
		try {
			markList = publicDictServer.selectMark(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public List<publicDictPojo> getList() {
		return list;
	}

	public void setList(List<publicDictPojo> list) {
		this.list = list;
	}

	public String getPublicCode() {
		return publicCode;
	}
	public void setPublicCode(String publicCode) {
		this.publicCode = publicCode;
	}

	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	public String getDelsString() {
		return delsString;
	}

	public void setDelsString(String delsString) {
		this.delsString = delsString;
	}
	public List<SystemParam> getSystemParamList() {
		return systemParamList;
	}
	public void setSystemParamList(List<SystemParam> systemParamList) {
		this.systemParamList = systemParamList;
	}
	public String getValueId() {
		return valueId;
	}
	public void setValueId(String valueId) {
		this.valueId = valueId;
	}
	public String getParamId() {
		return paramId;
	}
	public void setParamId(String paramId) {
		this.paramId = paramId;
	}
	public List<InquireMark> getMarkList() {
		return markList;
	}
	public void setMarkList(List<InquireMark> markList) {
		this.markList = markList;
	}
	public String getProm() {
		return prom;
	}
	public void setProm(String prom) {
		this.prom = prom;
	}
	
}
